$(function(){
    //initSubmitEvent();
    //初始化上传plupload控件
    createPlupload('browse','plate',uploadedImageCallback);
    createPlupload('uploadFile','plate',uploadLogoCallback);
});



function initSubmitEvent(){
    var submit = true;
    var  imageType =$("#imageList div.imageflag").find('#imageType').val();
    var  imageCount =$("#imageList div.imageflag");
    if (imageType==null){
        alert("请上传轮播图片");
        submit = false;
    }else if (imageCount.length>5){
        alert("最多只能上传5张轮播图片");
        submit = false;
    }
    if(submit){
        sub();
    }
}

function uploadLogoCallback(data){
    if(data.status == 200 ){
        data = $.parseJSON(data.response);
        var imagePath = imageServer +'/' + data.result[0].smallImageUrl;
        $("#fileImageTile").html("<span style='color:red'>*</span>logo缩略图：");
        var	innerHtml = '';
        innerHtml += '<img src="' + imagePath + '" width="80px"/>';
        innerHtml += '<input type="hidden" name="bigImageUrl" value="' + data.result[0].bigImageUrl+ '"/>';
        innerHtml += '<input type="hidden" name="smallImageUrl" value="' + data.result[0].smallImageUrl+ '"/>';
        innerHtml += '<input type="hidden" name="originalImageUrl" value="' + data.result[0].originalImageUrl+ '"/>';
        //图片赋值
        $("#fileImage").html(innerHtml);
    }
}

/**
 * 图片上传回显
 * @param data
 */
function uploadedImageCallback(data){
    if(data.status == 200 ){
        data = $.parseJSON(data.response);
        var imagePath = imageServer +'/' + data.result[0].smallImageUrl;
        var	innerHtml = '<div class="imageflag" title="双击图片进行删除" ondblclick="deleteCurrentImage(this);" style="display: inline-block" >';
        innerHtml += '<input type="hidden" id="imageType" name="imageList[][imageType]" value="0" />';
        innerHtml += '<img src="' + imagePath + '" width="80px"/>';
        innerHtml += '<input type="hidden" name="imageList[][bigImageUrl]" value="' + data.result[0].bigImageUrl+ '"/>';
        innerHtml += '<input type="hidden" name="imageList[][smallImageUrl]" value="' + data.result[0].smallImageUrl+ '"/>';
        innerHtml += '<input type="hidden" name="imageList[][originalImageUrl]" value="' + data.result[0].originalImageUrl+ '"/>';
        innerHtml += '</div>';
        //图片赋值
        $(innerHtml).prependTo('#imageList');
    }
}
/**
 * 删除/移除图片DOM元素
 */
function deleteCurrentImage(obj) {
    $(obj).remove();
}

function save(){
    var requestJson = $("form").serializeJSON();
    $.ajax({
        url: appServer + "/plate/edit.json",
        type: "POST",
        contentType : 'application/json;charset=utf-8', //设置请求头信息
        dataType:"json",
        data: JSON.stringify(requestJson),
        success: function(data){
            if(data.code == 0){
                alert(data.msg);
                window.location.href = appServer + "/plate/list.htm";
            }else{
                alert(data.msg);
            }
        },
        error: function(res){
            alert(res.responseText);
            inite();
        }
    });
}
function inite() {
    jQuery.removeData("disabledOnSubmit");
}

var result=true;
var response=true;
function validateSort(code){
    if(code != '') {
        jQuery.ajax({
            type:"post",
            url:appServer + "/plate/ajax/validateSort.htm",
            dataType:"json",
            data:{"code":code},
            success:function(message){
                var info = message.data;
                if(info=="N"){
                    $("#errorInfo").html("排序号已存在");
                    result=false;
                }else{
                    $("#errorInfo").html("");
                    result=true;
                }
            },
            error:function(xhr, message, e){

            }
        });
    }
}
function validateCode(code){
    if(code != '') {
        jQuery.ajax({
            type:"post",
            url:appServer + "/plate/ajax/validateCode.htm",
            dataType:"json",
            data:{"code":code},
            success:function(message){
                var info = message.data;
                if(info=="N"){
                    $("#codeErrorInfo").html("编码已存在");
                    response=false;
                }else{
                    $("#codeErrorInfo").html("");
                    response=true;
                }
            },
            error:function(xhr, message, e){

            }
        });
    }
}
function sub(){
    var id=$("#plateId").val();
    var plateName=$("#identityName").val();
    var plateSort=$("#plateSort").val();
    var plateDescription=$("#plateDescription").val();
    var discountRates = $("#discountRates").val();
    if(plateName=="" || plateName==null){
        alert("请填写专场名称");
        return;
    }
    if(plateSort=="" || plateSort==null){
        alert("请填写专场排序号");
        return;
    }
    if(plateDescription==""||plateDescription==null){
        alert("请填写专场描述");
        return;
    }
    if (discountRates && !/^(100|0|[1-9]\d?,){0,}(100|0|[1-9]\d?)$/.test(discountRates)) {
    	alert("请填写正确比例格式");
        return;
	}
    if(result && response){
        save();
    }
}